package cn.edu.cdu.group4nb.controller;

import cn.edu.cdu.group4nb.po.UserInf;
import cn.edu.cdu.group4nb.service.UserInfoService;
import cn.edu.cdu.group4nb.service.UserService;
import cn.edu.cdu.group4nb.utils.R;
import cn.edu.cdu.group4nb.utils.ShiroUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * @auther: tupeng
 * @date: 2019-10-09 12:32
 */
@Controller
public class RegisterController {
    private final UserService userService;

    @Autowired
    public RegisterController(UserService userService) {
        this.userService = userService;
    }

    //    public RegisterController(UserService userService) {
//        this.userService = userService;
//    }
    //注册
    @RequestMapping(value = "/sys/do_register")
    @ResponseBody
    public R register(@RequestBody Map<String, Object> map,
//                      @RequestParam("userid") String userid,
//                      @RequestParam("password") String password,
//                      @RequestParam("email") String email,
                      ModelAndView modelAndView) {
        String username = (String) map.get("username");
        String password = (String) map.get("password");
        String email = (String) map.get("email");
        System.out.println(username);
//        String passwd = userInf.getPassword();
//        String userid = userInf.getUserid();
        String passworda = new Md5Hash(password, username, 1024).toHex();
        UserInf userInf = new UserInf();
        userInf.setPassword(passworda);
        userInf.setUserid(username);
        userInf.setEmail(email);

        try {
            userService.Register(userInf);
        } catch (Exception e) {
            return R.error("注册失败");
        }

        System.out.println(userInf);
        return R.ok();
//        modelAndView.setViewName("/static/succeed.html");
//        return modelAndView;
    }

    //注册时检查用户名是否可用
    @RequestMapping(value = "/sys/do_checkUserName")
    @ResponseBody
    public boolean checkRegisterName(@RequestParam("userid") String userid) {
//        System.out.println(userid);
        boolean result = userService.checkUserName(userid);
//        System.out.println(result);
        return result;
//        return false;
    }
    @RequestMapping(value = "/sys/do_checkAddUser")
    @ResponseBody
    public boolean checkAddName(@RequestBody Map<String, Object> map) {
//        System.out.println(userid);
        String userid= (String) map.get("userid");
        boolean result = userService.checkUserName(userid);
//        System.out.println(result);
        return result;
//        return false;
    }

    @RequestMapping(value = "/sys/user/password", method = RequestMethod.POST)
    @ResponseBody
    public R updatePassword(@RequestParam("password") String password,
                            @RequestParam("newPassword") String newPassword,
                            @RequestParam("username") String username) {
        if (newPassword == null || "".equals(newPassword)) {
            return R.error("新密码不能为空");
        }
        String passwordT = password.replaceAll(" ", "");
        String newPasswordT = newPassword.replaceAll(" ", "");
        String usernameT = username.replaceAll(" ", "");
        String passworda = new Md5Hash(passwordT, usernameT, 1024).toHex();
        String newPassworda = new Md5Hash(newPasswordT, usernameT, 1024).toHex();
        UserInf userInf;
        userInf = ShiroUtils.getUserEntity();
        if (password.equals(newPassword)) {
            return R.error("原密码不能和新密码相同");
        }
        if (passworda.equals(userInf.getPassword())) {
            try {
                userService.updatePassword(newPassworda, username);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return R.ok();
        } else {
            return R.error("原密码不正确");
        }

//        return R.error("输入的原密码错误");
    }

}
